import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        //创建一个字符串 My name is "张三"
        String name = "My name is \"张三\"";




        int a = 10;
        {
            a = 99;
        }
        System.out.println(a);

    }

    /*public static void main29(String[] args) {
        {
            int a = 10;
            System.out.println(a); //编译通过
        }
        System.out.println(x); //编译失败，找不到变量x
    }*/




    public static void main28(String[] args) {
        int[][] array1 = {{1, 2, 3}, {4, 5, 6}};
        System.out.println(array1.length);
        System.out.println(array1[0].length);



        int[][] array2 = new int[][]{{1, 2, 3}, {1, 2, 3}};
        int[][] array3 = new int[2][3];

    }


    //给定一个整型数组，将所有的偶数放在前半部分，将所有的奇数放在数组后半部分

    public static void func5(int[] arr) {
        if (arr == null) return;
        int i = 0;
        int j = arr.length - 1;

        while (i < j) {
            // 从左往右找到第一个奇数
            while (i < j && arr[i] % 2 == 0) {
                i++;
            }
            // 从右到左找到第一个偶数
            while (i < j && arr[j] % 2 != 0) {
                j--;
            }
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
    }

    public static void main27(String[] args) {
        //int[] array = {1, 2, 3, 4, 5, 6};
        //int[] array = {1, 2, 3, 4};
        int[] array = {1, 2, 3, 4, 5, 6, 7};
        func5(array);
        System.out.println(Arrays.toString(array));

    }


    //通过数组名调用clone方法 — 克隆
    public static void main26(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        System.out.println(array);
        System.out.println(Arrays.toString(array));

        int[] copy = array.clone();
        System.out.println(copy);
        System.out.println(Arrays.toString(copy));
    }


    //直接使用System.arraycopy进行拷贝
    public static void main25(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        int[] copy = new int[array.length];
        System.arraycopy(array, 0, copy, 0, array.length);
        System.out.println(Arrays.toString(copy));
    }


    //用copyOf方法进行拷贝
    public static void main24(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        System.out.println(array);
        System.out.println(Arrays.toString(array));

        //int[] ret = Arrays.copyOf(array, array.length * 2);
        int[] ret = Arrays.copyOfRange(array, 2, 5);//左闭右开区间
        // java中见到from, to都是左闭右开区间
        System.out.println(ret);
        System.out.println(Arrays.toString(ret));

    }


    //模拟实现拷贝
    public static int[] copyArray(int[] array) {
        if (array == null) return null;

        int[] copy = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        return copy;
    }

    public static void main23(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        System.out.println(array);

        int[] ret = copyArray(array);
        System.out.println(ret);
        System.out.println(Arrays.toString(ret));
    }


    // 查找数组中指定元素(二分查找)
    public static int binarySearch(int[] arr, int key) {

        int i = 0;
        int j = arr.length - 1;

        while (i <= j) {
            int mid = (i + j) / 2;
            if (key < arr[mid]) {
                j = mid - 1;
            } else if (key > arr[mid]) {
                i = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main22(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        System.out.println(binarySearch(array, 1));
        System.out.println(binarySearch(array, 6));
        System.out.println(binarySearch(array, 9));

    }


    //查找数组中指定元素(顺序查找)
    //给定一个数组，再给定一个元素，找出该元素的数组中的位置
    public static int find(int[] arr, int key) {
        if (arr == null) {
            return -1;
        }

        // boolean flg = false;
        for (int i = 0; i < arr.length; i++) {
            if (key == arr[i]) {
                //System.out.println("在数组的下标为" + i + "处");
                //flg = true;
                return i;
            }
        }
        return -1;
        /*if(!flg) {
            System.out.println("该数组中没有这个元素");
        }*/
    }

    public static void main21(String[] args) {
        int[] array = {13, 6, 2, 5, 16};
        System.out.println(find(array, 2));

    }


    // 求整型数组平均数
    public static double avg(int[] arr) {
        if (arr == null) {
            return -1;
        }
        double sum = 0;
        /*for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }*/

        for (int x : arr) {
            sum += x;
        }

        //sum /= arr.length;
        return sum / arr.length;
    }


    public static void main20(String[] args) {
        int[] array = {13, 5, 2, 10, 16};
        System.out.println(avg(array));

    }


    //找到数组中的最大元素(最小元素同理)

    public static int maxNum(int[] arr) {
        if (arr == null) {
            return -1;
        }

        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        return max;
    }


    public static void main19(String[] args) {
        int[] array = {12, 5, 2, 10, 16};
        System.out.println(maxNum(array));

    }


    //模拟实现冒泡排序
    public static void bubbleSort(int[] arr) {
        if (arr == null) {
            // 实际上数组为null，是要抛出异常
            System.out.println("该数组为null");
        }

        // 控制比较的趟数
        for (int i = 0; i < arr.length - 1; i++) {

            boolean flg = false;// 又一次优化
            // 控制比较的次数  //优化，每次比上一次少1次比较
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    flg = true;
                }
            }
            // 判断如果比较一趟没有交换，就不用再继续比较
            if (!flg) {
                // 没有交换
                return;
            }
        }
    }

    public static void main18(String[] args) {
        int[] array = {22, 99, 88, 66, 55};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
        /*Arrays.sort(array);
        System.out.println(Arrays.toString(array));*/
    }


    //数组的逆序
    public static void reverse(int[] arr) {
        if (arr == null) {
            System.out.println("该数组为null");
        }
        int i = 0;
        int j = arr.length - 1;
        while (i < j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    }


    public static void main17(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        reverse(array);
        System.out.println(Arrays.toString(array));
    }


    public static void main16(String[] args) {
        int[] array = null;
        System.out.println(array);
    }


    // 模拟实现数组转字符串
    public static String myToString(int[] arr) {
        if (arr == null) {
            return "null";
        }

        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            str += arr[i];
            if (i < arr.length - 1) {
                str += ", ";
            }
        }
        str += "]";
        return str;
    }


    public static void main15(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        // System.out.println(Arrays.toString(array));

        System.out.println(myToString(array));

    }


    //数组作为方法的返回值
    //获取斐波那契数列的前N项
    public static int[] fib(int n) {
        if (n <= 0) {
            return null;
        }
        int[] array = new int[n];
        array[0] = array[1] = 1;
        for (int i = 2; i < n; ++i) {
            array[i] = array[i - 1] + array[i - 2];
        }
        return array;
    }

    public static void main14(String[] args) {
        int[] array = fib(10);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }


    public static void main13(String[] args) {
        int[] array = new int[]{1, 2, 3, 4, 5, 6};
        /*array = new int[5];
        array = new int[8];
        array = new int[12];*/
        System.out.println(array);
    }


    public static void main12(String[] args) {
        int[] array = {1, 2, 3, 4};
        System.out.println(array);
        int[] array2 = array;
        System.out.println(array2);
    }


    public static void func1(int[] array) {
        array = new int[]{11, 2, 13, 4, 51, 61};
    }

    public static void main10(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        func1(array);
        System.out.println(Arrays.toString(array));
    }

    public static void func2(int[] array) {
        array[0] = 899;
    }

    public static void main9(String[] args) {
        int[] arr = {1, 2, 3};
        func(arr);
        System.out.println("arr[0] = " + arr[0]);
    }

    public static void func(int[] a) {
        a[0] = 10;
        System.out.println("a[0] = " + a[0]);
    }

//    // 执行结果
//    a[0] = 10
//    arr[0] = 10


    // 数组转字符串,以字符串的形式打印数组
    public static void main8(String[] args) {
        int[] array = {11, 22, 3, 4, 5};

        //用的Arrays包中的方法
        //String ret = Arrays.toString(array);
        System.out.println(Arrays.toString(array));

        //数组中元素快速排序
        //Arrays.sort(array);
        //区间快速排序
        /*Arrays.sort(array,0,4); //[) 左闭右开区间
        System.out.println(Arrays.toString(array));*/
    }


    // 数组作为方法的返回值
    public static int[] func3() {
        int[] ret = new int[2];
        ret[0] = 88;
        ret[1] = 99;
        return ret;
    }

    public static void main7(String[] args) {
        int[] ret = func3();
        for (int x : ret) {
            System.out.println(x);
        }

    }


    // 数组作为方法的参数进行传递
    /*public static void func1(int[] array) {
        array[0] = 99;
    }

    public static void func2(int[] array) {
        array = new int[]{11,22,33,44,55};
    }*/


    public static void main6(String[] args) {
        int[] array = {1, 2, 3, 4};

        /*func1(array);
        for (int x: array) {
            System.out.print(x + " ");
        }*/

        //func2(array);
        for (int x : array) {
            System.out.print(x + " ");
        }
    }


    public static void main5(String[] args) {
        /*int[] array = null;
        System.out.println(array.length);*/


        int[] array2 = {1, 2, 3};
        int[] array3 = array2;
        System.out.println(array2);
        System.out.println(array3);

    }


    public static void main4(String[] args) {
        int[] array = {1, 2, 3, 4};

        // 通过for循环遍历数组
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }

        System.out.println();

        //通过增强for循环（for-each）遍历数组
        for (int x : array) {
            System.out.print(x + " ");
            ;
        }
    }


    public static void main3(String[] args) {
        int[] array3 = null;
        System.out.println(array3);


        //不能这样赋值，数组整体赋值只有一次机会，在定义的时候
        //int[] array3 = {1,2,3,4};
//        int[] array3; //局部变量
//        array3 = {1,2,3,4};


        int[] array2;
        array2 = new int[]{1, 2, 3};


        int[] array;
        array = new int[10];
        System.out.println(array);

    }


    public static void main2(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        //求数组长度
        System.out.println(array.length);


        int n = 10;
        int[] array3 = new int[n];
        //可以单个初始化
        array3[5] = 88;
        array3[6] = 99;
        //也可以通过for循环初始化，可以根据业务场景进行初始化


        /*System.out.println(array[0]);
        System.out.println(array[1]);
        System.out.println(array[2]);
        System.out.println(array[3]);*/
        //System.out.println(array[12]);


    }


    public static void main1(String[] args) {
        int[] array = null;
        System.out.println(array[0]);


        /*int[] array = {1, 2, 3};
        System.out.println(array.length);

        System.out.println(array[5]); // 数组中只有3个元素，下标一次为：0 1 2，array[5]下标越界*/


        // 定义数组的三种方式
        //int[] array = {1,2,3,4,5};
        // int array2[] = {1,2,3,4,5}; //不建议这样的写法

        int[] array2 = new int[]{1, 2, 3, 4, 5};

        // int[] array3 = new int[10]{1,2,3,4,5};

        long[] array5 = {1, 2, 3};

        float[] array4 = {(float) 1.2, (float) 5.2};


        /*byte[] arr1 = {1,2,3,4};
        float[] arr2 = {1.2, 2.3}; // 报错
        float[] arr3 = {(float)1.2, (float) 2.3}; // 强转没问题

        float[] arr4 = new float[] {1.2, 2.3}; // 报错
        float[] arr5 = new float[] {(float)1.2, (float) 2.3}; // 强转没问题

        float[] arr6 = new float[2]; //动态初始化也可以*/


//        long[] arr2 = new long[] {2, 4};
//        float[] arr3 = new float[10];
//        long[] arr4 = new long[10];
//        char[] arr5 = {'a', 'b'};
//        boolean[] arr6 = {true,false};
//        boolean[] arr = new boolean[] {true,false};


    }


}
